package com.meichis.yndms.ui.activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meichis.mcsappframework.adapter.recyclerview.MultiItemTypeAdapter;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.StayInOrderAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityStayInOrderBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.StayInOrderVM;

import java.util.Calendar;

public class StayInOrderActivity extends BaseActivity {

    private ActivityStayInOrderBinding binding;
    private StayInOrderVM vm;
    private StayInOrderAdapter adapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_stay_in_order);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(StayInOrderVM.class);
        binding.setClick(new ClickEvent());
        binding.setVm(vm);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("在途订单");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        addObserver();
        vm.getPartnerStayIn(vm.getBeginTime().getValue(), vm.getEndTime().getValue());
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getAllDatas().observe(this, stayInOrders -> {
            if (stayInOrders.size() < 1) {
                return;
            }
            adapter = new StayInOrderAdapter(this, R.layout.item_stay_in_order, stayInOrders);
            binding.rc.setLayoutManager(new LinearLayoutManager(this));
            binding.rc.setAdapter(adapter);
            adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(MCode.STAYINORDER, vm.getAllDatas().getValue().get(position));
                    openActivity(StayInOrderDetailActivity.class, bundle);
                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                    return false;
                }
            });
        });
    }

    public class ClickEvent {

        public void selectBeginTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(StayInOrderActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getBeginTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void selectEndTime() {
            final Calendar cd = Calendar.getInstance();
            new DatePickerDialog(StayInOrderActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    cd.set(Calendar.YEAR, year);
                    cd.set(Calendar.MONTH, monthOfYear);
                    cd.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    vm.getEndTime().setValue(DateUtil.date2Str(cd, DateUtil.FORMAT_YMD));
                }
            }, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
        }

        public void getPartnerStayIn() {
            vm.getPartnerStayIn(vm.getBeginTime().getValue(), vm.getEndTime().getValue());
        }

    }
}
